package cn.iocoder.yudao.module.erp.controller.admin.factorycalendar;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.factorycalendar.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.factorycalendar.FactoryCalendarDO;
import cn.iocoder.yudao.module.erp.service.factorycalendar.FactoryCalendarService;

@Tag(name = "管理后台 - ERP工厂日历")
@RestController
@RequestMapping("/erp/factory-calendar")
@Validated
public class FactoryCalendarController {

    @Resource
    private FactoryCalendarService factoryCalendarService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP工厂日历")
    @PreAuthorize("@ss.hasPermission('erp:factory-calendar:create')")
//    public CommonResult<Long> createFactoryCalendar(@Valid @RequestBody FactoryCalendarSaveReqVO createReqVO) {
//        return success(factoryCalendarService.createFactoryCalendar(createReqVO));
//    }
    public CommonResult<List<Long>> createFactoryCalendar(@Valid @RequestBody List<FactoryCalendarSaveReqVO> createReqVOList) {
        return success(factoryCalendarService.createFactoryCalendars(createReqVOList));
    }
    @PutMapping("/update")
    @Operation(summary = "更新ERP工厂日历")
    @PreAuthorize("@ss.hasPermission('erp:factory-calendar:update')")
    public CommonResult<Boolean> updateFactoryCalendar(@Valid @RequestBody FactoryCalendarSaveReqVO updateReqVO) {
        factoryCalendarService.updateFactoryCalendar(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP工厂日历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:factory-calendar:delete')")
    public CommonResult<Boolean> deleteFactoryCalendar(@RequestParam("id") Long id) {
        factoryCalendarService.deleteFactoryCalendar(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP工厂日历")
    @Parameter(name = "fyear", description = "年", required = true, example = "2025")
    @PreAuthorize("@ss.hasPermission('erp:factory-calendar:query')")
    public CommonResult<List<FactoryCalendarRespVO>> getFactoryCalendar(@RequestParam("fyear") String fyear) {
        List<FactoryCalendarDO> factoryCalendars = factoryCalendarService.getFactoryCalendar(fyear);
        return success(BeanUtils.toBean(factoryCalendars, FactoryCalendarRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP工厂日历分页")
    @PreAuthorize("@ss.hasPermission('erp:factory-calendar:query')")
    public CommonResult<PageResult<FactoryCalendarRespVO>> getFactoryCalendarPage(@Valid FactoryCalendarPageReqVO pageReqVO) {
        PageResult<FactoryCalendarDO> pageResult = factoryCalendarService.getFactoryCalendarPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FactoryCalendarRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP工厂日历 Excel")
    @PreAuthorize("@ss.hasPermission('erp:factory-calendar:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFactoryCalendarExcel(@Valid FactoryCalendarPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryCalendarDO> list = factoryCalendarService.getFactoryCalendarPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP工厂日历.xls", "数据", FactoryCalendarRespVO.class,
                        BeanUtils.toBean(list, FactoryCalendarRespVO.class));
    }

}